﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIDesigner.SurfaceProperties;
using SharedClasses;
using System.Xml.Serialization;
using System.IO;
using System.Collections;
using System.Diagnostics;

namespace UIDesigner.Designers.Forms
{
    public partial class frmSurfaceProperties : Form
    {
        private SurfacePropertiesBase m_objProperties;
        private SurfaceActionType m_objSurfaceActionType;
        private SurfaceObjectType m_objSurfaceObjectType;
        private SurfacePropertiesBase m_objSelectedProperties;

        public frmSurfaceProperties(SurfaceActionType objSurfaceActionType, SurfacePropertiesBase objProperties)
        {
            m_objProperties = (SurfacePropertiesBase) objProperties.Clone();
            m_objSurfaceActionType = objSurfaceActionType;

            InitializeComponent();
        }

        private void frmSurfaceProperties_Load(object sender, EventArgs e)
        {
            lblPropertiesOf.Text = "Properties of " + m_objSurfaceActionType.BaseObjectControl.Name;
            lblCurrentlySelected.Text = "Currently selected: " + SavedSurfaceProperties.AppySelectedText;

            m_objSurfaceObjectType = new SurfaceObjectType(m_objSurfaceActionType.Host, m_objSurfaceActionType.BaseObjectControl, m_objProperties);

            PropertyGrid.SelectedObject = m_objSurfaceObjectType;
            PropertyGrid.PropertyTabs.AddTabType(typeof(SurfacePropertiesTab), PropertyTabScope.Static);

            WindowsAPI.SendMessage(PropertyGrid.Handle.ToInt32(), (int)WindowsAPI.PropertyGridMessage.PG_CHANGE_TAB, 4, 0);

            if (SavedSurfaceProperties.FileExists)
            {
                SavedSurfaceProperties.LoadFile();
                LoadList();
            }
        }

        public void LoadList()
        {
            lvwPropertyGroups.Items.Clear();

            foreach (SurfacePropertiesBase objSurfaceProperties in SavedSurfaceProperties.PropertyGroups)
            {
                ListViewItem objItem = lvwPropertyGroups.Items.Add(objSurfaceProperties.Name);

                if (objSurfaceProperties is FormSurfaceProperties)
                {
                    objItem.SubItems.Add("Form");
                }
                else if (objSurfaceProperties is ControlSurfaceProperties)
                {
                    objItem.SubItems.Add("Control");
                }
                else
                {
                    Debugger.Break();
                }

                objItem.SubItems.Add(objSurfaceProperties.Description);
            }

            txtName.Focus();
        }

        public SurfacePropertiesBase SelectedProperties
        {
            get 
            { 
                return m_objSelectedProperties; 
            }
        }

        private void cmdSaveToList_Click(object sender, EventArgs e)
        {
            if (SavedSurfaceProperties.PropertyGroups.Count > 0)
            {
                foreach (SurfacePropertiesBase objSurfaceProperties in SavedSurfaceProperties.PropertyGroups)
                {
                    if (objSurfaceProperties != m_objProperties)
                    {
                        if (objSurfaceProperties.Name == txtName.Text)
                        {
                            MessageBox.Show("Item with specified name already exists.  Please enter a unique name");
                            return;
                        }
                    }
                }
            }

            m_objProperties.Name = txtName.Text;
            m_objProperties.Description = txtDescription.Text;

            if (!SavedSurfaceProperties.PropertyGroups.Contains(m_objProperties.Name))
            {
                SavedSurfaceProperties.PropertyGroups.Add(m_objProperties);
            }

            SaveAndLoad();

            txtName.Text = "";
            txtDescription.Text = ""; 
        }

        private void SaveAndLoad()
        {
            SavedSurfaceProperties.SaveFile();
            SavedSurfaceProperties.LoadFile();

            LoadList();
        }

        private void lvwPropertyGroups_DoubleClick(object sender, EventArgs e)
        {
            ListViewItem objItem = lvwPropertyGroups.SelectedItems[0];

            cmdApply.Enabled = true;

            foreach (SurfacePropertiesBase objSurfaceProperties in SavedSurfaceProperties.PropertyGroups)
            {
                if (objSurfaceProperties.Name == objItem.Text)
                {
                    lblPropertiesOf.Text = "Properties of " + objSurfaceProperties.Name;

                    m_objProperties = objSurfaceProperties;
                    m_objSurfaceObjectType = new SurfaceObjectType(m_objSurfaceActionType.Host, m_objSurfaceActionType.BaseObjectControl, m_objProperties);

                    PropertyGrid.SelectedObject = m_objSurfaceObjectType;

                    cmdSaveToList.Text = "Save";
                    cmdSaveToList.Enabled = true;

                    txtName.Text = objSurfaceProperties.Name;
                    txtDescription.Text = objSurfaceProperties.Description;

                    lblInstructions.Text = "You are editing an existing Property Group. Click 'Save' to save changes.";
                    lblSelected.Text = objSurfaceProperties.Name;

                    m_objSelectedProperties = objSurfaceProperties;

                    break;
                }
            }
        }

        private void lvwPropertyGroups_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                ListViewItem objItem = lvwPropertyGroups.SelectedItems[0];
                SurfacePropertiesBase objDelete = null;

                foreach (SurfacePropertiesBase objSurfaceProperties in SavedSurfaceProperties.PropertyGroups)
                {
                    if (objSurfaceProperties.Name == objItem.Text)
                    {
                        objDelete = objSurfaceProperties;
                        break;
                    }
                }

                if (objDelete != null)
                {
                    if (MessageBox.Show("Are you sure you want to delete '" + objDelete.Name + "'?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        SavedSurfaceProperties.PropertyGroups.Remove(objDelete);
                        SaveAndLoad();

                        cmdApply.Enabled = false;
                    }
                }
            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            if (txtName.Text.Length > 0)
            {
                cmdSaveToList.Enabled = true;
            }
            else
            {
                cmdSaveToList.Enabled = false;
            }
        }

        private void lblSelected_TextChanged(object sender, EventArgs e)
        {
            if (lblSelected.Text.Length > 0)
            {
                lblArrow.Visible = true;
            }
        }

        private void cmdApply_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
